package com.one.api.push.email.sender;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.one.api.common.constant.BeanNameConstant;
import com.one.api.common.msg.entity.body.EmailMsgBody;
import com.one.api.common.util.SpringContextUtil;
import com.one.api.push.config.ApiPushConfigure;

public class AliEmailUtil {
    public static void sendSingleEmail(EmailMsgBody msgBody){
        // 如果是除杭州region外的其它region（如新加坡、澳洲Region），需要将下面的"cn-hangzhou"替换为"ap-southeast-1"、或"ap-southeast-2"。
        ApiPushConfigure configure=SpringContextUtil.getBean(BeanNameConstant.apiPushConfigure, ApiPushConfigure.class);
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", configure.getAliAccessKeyId(), configure.getAliAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        SingleSendMailRequest request = new SingleSendMailRequest();
        request.setAccountName(configure.getAliEmailSendAddr());
        request.setFromAlias(configure.getAliEmailFromAlias());
        request.setAddressType(1);
        request.setTagName(configure.getAliEmailTagName());
        request.setReplyToAddress(true);
        request.setToAddress(msgBody.getToAddress());
        //可以给多个收件人发送邮件，收件人之间用逗号分开，批量发信建议使用BatchSendMailRequest方式
        //request.setToAddress("邮箱1,邮箱2");
        request.setSubject(msgBody.getSubject());
        request.setHtmlBody(msgBody.getContent());
        SingleSendMailResponse response=null;
        try {
            //request.setVersion("2017-06-22");// 如果是除杭州region外的其它region（如新加坡region）,必须指定为2017-06-22
            response = client.getAcsResponse(request);

        } catch (ServerException e) {
            e.printStackTrace();
        }
        catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
